<?php

namespace app\common\model;

use think\Model;

class UserVideo extends Model
{
    // 表名
    protected $name = 'user_video';




    /**
     * 更新视频课资源
     * @param mixed $user_id
     * @param mixed $video_id
     * @param mixed $teacher_id
     * @param mixed $day
     * @return void
     */
    public function saveUserVideo($user_id, $video_id, $teacher_id, $day = 365)
    {
        $info = self::where([
            'user_id' => $user_id,
            'video_id' => $video_id,
            'teacher_id' => $teacher_id
        ])->find();
        if (empty($info)) {
            $end_time = time() + $day * 86400;
            $data = [
                'user_id' => $user_id,
                'teacher_id' => $teacher_id,
                'video_id' => $video_id,
                'end_time' => $end_time,
                'createtime' => time(),
                'buy_is' => '1',
            ];
            self::create($data);
        } else {
            $time = $info->end_time > time() ? $info->end_time + $day * 86400 : time() + $day * 86400;
            $info->end_time = $time;
            $info->save();
        }
    }
}
